package me.android.library.common.orm.ability;

import android.database.sqlite.SQLiteDatabase;

import com.google.common.base.Joiner;

import java.util.Collection;
import java.util.stream.Collectors;

import mylab.base.foundation.lang.Identifiable;

public interface Delete<T extends Identifiable<Long>> extends DS, EntityInfo<T> {
    default boolean deleteById(Long id) {
        String where = "id = ?";
        String[] args = new String[]{String.valueOf(id)};
        return delete(where, args) == 1;
    }

    default int delete(Collection<Long> ids) {
        String where = "id in (?)";
        String idsStr = Joiner.on(",").skipNulls().join(ids.stream().map(String::valueOf).collect(Collectors.toSet()));
        String[] args = new String[]{idsStr};
        return delete(where, args);
    }

    default int deleteAll() {
        return delete(null, null);
    }

    default int delete(String whereClause, String[] whereArgs) {
        EntityInfo.checkWhere(whereClause, whereArgs);
        try (SQLiteDatabase db = writableDb()) {
            return db.delete(getTableName(), whereClause, whereArgs);
        }
    }

}
